package com.chen.palmar.project.set;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.chen.palmar.R;
import com.chen.palmar.base.BaseActivity;
import com.chen.palmar.common.constant.AppConstant;
import com.chen.palmar.common.datasource.DataCenter;
import com.chen.palmar.common.override.HttpSubscriber;
import com.chen.palmar.common.widget.view.AutoToolbar;
import com.chen.palmar.common.widget.view.RoundImageView;
import com.chen.palmar.entity.NewListEntity;
import com.flyco.dialog.listener.OnBtnClickL;
import com.flyco.dialog.widget.NormalDialog;
import com.primb.androidlibs.net.entity.HttpResultEntity;
import com.primb.androidlibs.ui.recyclerview.BaseQuickAdapter;
import com.primb.androidlibs.ui.recyclerview.BaseViewHolder;
import com.squareup.picasso.Picasso;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.OnClick;
import rx.Subscription;

/**
 * @author Chen
 * @date 2018/1/10
 * 功能描述：
 */

public class StoreManagerActivity extends BaseActivity {
    @Bind(R.id.title_bar)
    TextView titleBar;
    @Bind(R.id.tool_bar)
    AutoToolbar toolBar;
    @Bind(R.id.rv_add_store_list)
    RecyclerView rvAddStoreList;
    @Bind(R.id.tv_add_store)
    TextView tvAddStore;
    private static final int REQUEST_CODE_ADD = 51;
    private BaseQuickAdapter<NewListEntity.DataBean, BaseViewHolder> adapter;
    private String info;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    @Override
    public void init() {
        toolBar.setNavigationOnClickListener(v -> StoreManagerActivity.this.finish());

        titleBar.setText("商品管理");

        info = getIntent().getStringExtra("info");

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rvAddStoreList.setLayoutManager(layoutManager);
        rvAddStoreList.setItemAnimator(new DefaultItemAnimator());
        adapter = new BaseQuickAdapter<NewListEntity.DataBean, BaseViewHolder>(R.layout.item_manager_store) {
            @Override
            protected void convert(BaseViewHolder helper, NewListEntity.DataBean item) {
                helper.setText(R.id.tv_item_store_name, item.getTitle());
                helper.setText(R.id.tv_item_store_price, item.getGuidance_price()+"元");
                RoundImageView view = (RoundImageView) helper.getView(R.id.iv_item_store_img);
                Picasso.with(StoreManagerActivity.this).load(item.getImg()).resize(AppConstant.SCREEN_WIDTH / 2, AppConstant.SCREEN_WIDTH / 2).placeholder(R.mipmap.home_item_head_icon).error(R.mipmap.home_item_head_icon).into(view);

                helper.addOnClickListener(R.id.tv_item_store_new);
                helper.addOnClickListener(R.id.tv_item_store_edit);
                helper.addOnClickListener(R.id.tv_item_store_delete);
            }
        };
        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.tv_item_store_new:

                        break;
                    case R.id.tv_item_store_edit:
                        Intent intent = new Intent(StoreManagerActivity.this, ApplyStoreActivity.class);
                        intent.putExtra("info", ((NewListEntity.DataBean) adapter.getData().get(position)));
                        intent.putExtra("store", info);
                        startActivityForResult(intent, REQUEST_CODE_ADD);
                        break;
                    case R.id.tv_item_store_delete:
                        deleteStore(((NewListEntity.DataBean) adapter.getData().get(position)).getProductId(), position);
                        break;
                }
            }
        });

        rvAddStoreList.setAdapter(adapter);

        try {
            getListData();
        } catch (Exception e) {
            e.printStackTrace();
            showToast("服务器数据异常");
        }
    }

    private void deleteStore(int productId, int index) {
        NormalDialog dialog = showAlterDialog("删除提示", "是否删除该商品");
        dialog.btnText("取消","删除");
        dialog.setOnBtnClickL(null, new OnBtnClickL() {
            @Override
            public void onBtnClick() {
                HashMap<String, String> map = new HashMap<>();
                map.put("product_id", productId + "");
                Subscription subscribe = DataCenter.deleteStore(map).subscribe(new HttpSubscriber<HttpResultEntity>(StoreManagerActivity.this) {
                    @Override
                    public void onNext(HttpResultEntity entity) {
                        showToast(entity.getMessage());
                        adapter.remove(index);
                    }
                });
                subscription.add(subscribe);
            }
        });
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_store_manager;
    }

    @OnClick(R.id.tv_add_store)
    public void onViewClicked() {
        Intent intent = new Intent(this, ApplyStoreActivity.class);
        intent.putExtra("store", info);
        startActivityForResult(intent, REQUEST_CODE_ADD);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_ADD && resultCode == RESULT_OK) {
            getListData();
        }
    }

    private void getListData() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("store_id", info);
        Subscription subscribe = DataCenter.storeList(map).subscribe(new HttpSubscriber<NewListEntity>(this, showLoading()) {

            @Override
            public void onNext(NewListEntity entity) {
                adapter.setNewData(entity.getData());
            }
        });
        subscription.add(subscribe);
    }
}
